package com.tfswue.ershoujiaoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tfswue.ershoujiaoyi.entity.Review;
import com.tfswue.ershoujiaoyi.mapper.ReviewMapper;
import com.tfswue.ershoujiaoyi.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private ReviewMapper reviewMapper;

    @Override
    public Review addReview(Review review) {
        reviewMapper.insert(review);
        return review;
    }

    @Override
    public List<Review> listByProduct(Long productId) {
        return reviewMapper.selectList(new QueryWrapper<Review>().eq("product_id", productId));
    }

    @Override
    public List<Review> listAll() {
        return reviewMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public Review getById(Long id) {
        return reviewMapper.selectById(id);
    }

    @Override
    public Review update(Review review) {
        reviewMapper.updateById(review);
        return reviewMapper.selectById(review.getId());
    }

    @Override
    public boolean delete(Long id) {
        return reviewMapper.deleteById(id) > 0;
    }
}
